package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * projectName: tingshu-parent
 *
 * @author: 王宝贵
 * time: 2024/10/28 21:16 周一
 * description:
 */
@Component
@Slf4j
public class SearchReceiver {

    @Autowired
    private SearchService searchService;


    /**
     * 专辑上架
     * @param albumId
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            key = {MqConst.ROUTING_ALBUM_UPPER}
    ))

    @SneakyThrows
    public void albumUpper(Long albumId, Message message, Channel channel){

        //判断 setnx
        if(albumId!=null){
            searchService.upperAlbum(albumId);
        }

        //手机确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



    /**
     * 专辑下架
     * @param albumId
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            key = {MqConst.ROUTING_ALBUM_LOWER}
    ))

    @SneakyThrows
    public void lowerAlbum(Long albumId, Message message, Channel channel){

        //判断 setnx
        if(albumId!=null){
            searchService.lowerAlbum(albumId);
        }

        //手机确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


}